<template>
  <avue-crud ref="crud"
             :data="modelValue"
             v-model="form"
             :option="option"
             @row-save="rowSave"
             @row-update="rowUpdate"
             @row-del="rowDel"></avue-crud>
</template>
<script>

export default {
  name: "mqttTable",
  props: {
    modelValue: {
      type: Object,
      default: () => ([])
    },
    payload: {
      type: Boolean,
      default: false
    },
    viewMode: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {},
      data: [],
      option: {
        header: false,
        menuWidth: 180,
        column: [
          {
            label: "Topic",
            prop: "topic",
          },
          {
            label: "QoS",
            prop: "qos",
            width: 120,
          },
          {
            label: "Payload",
            prop: "payload",
            hide: true,
          },
          {
            label: "时间",
            prop: "time",
            width: 180,
          },
        ]
      },
    };
  },
  mounted() {
    // 在这里可以安全地进行DOM操作
    this.$nextTick(() => {
      // 代码在这里确保整个视图已经更新完毕
      if (this.viewMode) {
        this.$refs.crud.option.editBtn = false;
        this.$refs.crud.option.delBtn = false;
        this.$refs.crud.option.menu = false;
        this.findColumn("qos").hide = true;
      } else {
        this.$refs.crud.option.editBtn = false;
        this.$refs.crud.option.delBtn = true;
        this.$refs.crud.option.menu = true;
      }
      this.findColumn("payload").hide = !this.payload;
    });
  },
  methods: {
    findColumn(prop) {
      return this.$refs.crud.option.column.find(item => item.prop === prop);
    },
    rowSave(form, done, loading) {
      form.id = new Date().getTime();
      loading();
      done(form);
    },
    rowUpdate(form, index, done, loading) {
      loading();
      done(form);
    },
    rowDel(form, index, done) {
      this.$confirm('确定将选择数据删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        done(form)
        this.$emit('onRemove', form);
      });
    },
  }
}
</script>
